<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 19-1-17
 * Time: 上午10:04
 */

namespace common\models\db;


/*use common\components\tools\ArrayHelper;
use common\members\wodrow\validators\Loop;
use common\members\wodrow\widgets\tree\TreeBehavior;*/

use common\components\tools\ArrayHelper;
use wodrow\yii2wwtree\Loop;
use wodrow\yii2wwtree\TreeBehavior;

/**
 * Class TestTt
 * @package common\models\db
 *
 * @property TestTt $p
 * @property TestTt[] $childs
 */
class TestTt extends \common\models\db\tables\TestTt
{
    public function behaviors()
    {
        return [
            'tree' => [
                'class' => TreeBehavior::class,
            ],
        ];
    }

    public function rules()
    {
        $r = parent::rules();
        foreach ($r as $k => $v){
            if ($v[1] == 'required'){
                if (is_array($v[0])){
                    $a1[$k][0] = array_diff($r[$k][0], ['created_at', 'updated_at', 'created_by', 'updated_by', 'uuid']);
                }
            }
        }
        $rules = [
            ['pid', Loop::class],
        ];
        $rules = ArrayHelper::merge($r, $rules);
        return $rules;
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getP()
    {
        return $this->hasOne(self::class, ['id' => 'pid']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getChilds()
    {
        return $this->hasMany(self::class, ['pid' => 'id']);
    }

    public function afterDelete()
    {
        parent::afterDelete();
        $this->afMove();
        foreach ($this->childs as $k => $v) {
            $v->delete();
        }
    }
}